<template>
  <div class="app-container">
    <el-form :model="form" :rules="rules" :inline="true" label-width="100px">
      <el-row :gutter="40">
        <el-col :span="40">
          <el-form-item label="应用" prop="pkg">
            <el-select v-model="form.pkg" placeholder="应用" filterable clearable style="width: 100%">
              <el-option
                v-for="item in productList"
                :key="item.value"
                :label="item.name"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="40">
          <el-form-item label="渠道" prop="channel">
            <el-select v-model="form.channel" placeholder="渠道" filterable clearable style="width: 100%">
              <el-option
              v-for="item in dict.type.sys_channel"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="40">
          <el-form-item label="oaid" prop="oaid">
            <el-input
              v-model.trim="form.oaid"
              placeholder="请输入oaid"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="40">
          <el-form-item label="回传类型" prop="type">
            <el-select v-model="form.type" placeholder="回传类型" filterable clearable style="width: 100%">
              <el-option
              v-for="item in uploadTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="40">
          <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { upload } from "@/api/configs/compensate";
import { getAllAppList,selectAppList } from '@/api/configs/appConfig';

export default {
  dicts: ['sys_channel'],
  data() {
    return {
      productList: [],
      channelMap: {},
      uploadTypeMap: {},
      uploadTypeList: [{"label":"激活","value":"1"},{"label":"注册","value":"3"},{"label":"付费","value":"4"},{"label":"关键行为","value":"5"}],
      // 查询参数
      form: {
        pkg: '',
        channel: '',
        oaid: '',
        type: '',
      },
      rules: {
        pkg: [{ required: true, message: '应用不能为空', trigger: 'change' }],
        channel: [{ required: true, message: '渠道不能为空', trigger: 'change' }],
        oaid: [{ required: true, message: 'oaid不能为空', trigger: 'change' }],
        type: [{ required: true, message: '回传类型不能为空', trigger: 'change' }],
      },
    };
  },
  created() {
    this.init();
  },
  methods: {
    // 初始化
    init () {
      //查询应用列表
      getAllAppList().then(response => {
        this.productList = response.data.map(item => {
          return {
            name: item.appname,
            value: item.apppkg
          }
        })
      })
    },
    // 提交
    submitForm() {
      let vm = this;
      let prompt = '请确认补偿信息';
      vm.$modal.confirm(prompt).then(() => {
        vm.loading = true;
        return upload(this.form);
      }).then(response => {
        if (response.code === 200) {
          vm.$modal.msgSuccess('操作成功');
        } else {
          vm.$modal.msgError(response.msg);
        }
      }).catch(() => {
        vm.loading = false;
      });
    }
  }
}
</script>
